<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小方狗果蔬识别系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f4f8;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            color: #333;
        }
        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 350px;
            text-align: center;
            border: 1px solid #e0e0e0;
        }
        h1 {
            font-size: 24px;
            margin-bottom: 20px;
            color: #3498db;
        }
        input[type="file"],
        input[type="text"],
        button {
            width: calc(100% - 22px);
            padding: 12px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 6px;
            font-size: 16px;
        }
        button {
            background-color: #2ecc71;
            color: #fff;
            border: none;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        button:hover {
            background-color: #27ae60;
        }
        #result {
            margin-top: 20px;
            padding: 15px;
            border: 1px solid #e0e0e0;
            border-radius: 6px;
            background-color: #f9f9f9;
            font-size: 16px;
            position: relative;
        }
        #result.error::before {
            content: "\f071";
            font-family: 'Font Awesome 6 Free';
            font-weight: 900;
            margin-right: 8px;
            color: red;
        }
        #result.success::before {
            content: "\f00c";
            font-family: 'Font Awesome 6 Free';
            font-weight: 900;
            margin-right: 8px;
            color: green;
        }
        .loading {
            display: none;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>小方狗果蔬识别系统</h1>
        <form id="uploadForm" enctype="multipart/form-data">
            <input type="file" id="imageInput" name="file" accept="image/*" required>
            <input type="text" id="queryInput" name="query" placeholder="请输入您的问题" required>
            <button type="submit">提交</button>
        </form>
        <div id="result"></div>
        <div class="loading">
            <i class="fas fa-spinner fa-spin"></i> 稍微等一下叭...
        </div>
    </div>

    <script>
        document.getElementById('uploadForm').addEventListener('submit', function(event) {
            event.preventDefault();

            const formData = new FormData(this);
            const resultDiv = document.getElementById('result');
            const loadingDiv = document.querySelector('.loading');

            resultDiv.classList.remove('error', 'success');
            resultDiv.innerText = '';
            loadingDiv.style.display = 'block';

            fetch('/upload', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                loadingDiv.style.display = 'none';
                if (data.error) {
                    resultDiv.classList.add('error');
                    resultDiv.innerText = `错误: ${data.error}`;
                } else {
                    resultDiv.classList.add('success');
                    resultDiv.innerText = `答案: ${data.answer}`;
                }
            })
            .catch(error => {
                console.error('Error:', error);
                loadingDiv.style.display = 'none';
                resultDiv.classList.add('error');
                resultDiv.innerText = '处理请求时发生错误。';
            });
        });
    </script>
</body>
</html>